package com.wsoft.form.controller;

import com.alibaba.fastjson.JSONObject;
import com.wsoft.form.entity.WfFormDesignEntity;
import com.wsoft.form.query.design.DesignInsertQuery;
import com.wsoft.form.query.design.DesignListQuery;
import com.wsoft.form.query.design.DesignUpdateQuery;
import com.wsoft.form.service.IWfFormDesignService;
import com.wsoft.response.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 自定义表单-设计表 前端控制器
 * </p>
 *
 * @author Z001
 * @since 2024年08月29日 09:11:06
 */
@RestController
@Tag(name = "设计表")
@RequestMapping("/wf/design")
public class WfFormDesignController {

    @Resource
    private IWfFormDesignService formDesignService;


    @Operation(summary = "新增自定义表单", description = "新增自定义表单")
    @PostMapping("/insert")
    public ResponseResult insert(@RequestBody @Valid DesignInsertQuery query) {
        boolean b = formDesignService.insertFormDesign(query);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "修改自定义表单", description = "修改自定义表单")
    @PostMapping("/update")
    public ResponseResult updateFormDesign(@RequestBody @Valid DesignUpdateQuery query) {
        boolean b = formDesignService.updateFormDesign(query);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "自定义表单列表", description = "自定义表单列表")
    @PostMapping("/list")
    public ResponseResult list(@RequestBody DesignListQuery query) {
        List<WfFormDesignEntity> list = formDesignService.listFormDesign(query);
        return ResponseResult.success(list);
    }

    @Operation(summary = "删除自定义表单", description = "删除自定义表单")
    @GetMapping("/delFormDesign")
    public ResponseResult delFormDesign(@RequestParam("id") Long id) {
        boolean b = formDesignService.delFormDesign(id);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "获取自定义表单详情", description = "获取自定义表单详情")
    @GetMapping("/get")
    public ResponseResult get(@RequestParam("id") Long id) {
        return ResponseResult.success(formDesignService.get(id));
    }


    @Operation(summary = "获取指定在线表单对象在前端渲染时所需的所有数据对象", description = "获取指定在线表单对象在前端渲染时所需的所有数据对象")
    @GetMapping("/render")
    public ResponseResult render(@RequestParam("id") Long designId) {
        JSONObject json = formDesignService.render(designId);
        return ResponseResult.success(json);
    }

}
